/********************************************************************************
* Project       : No Latency RTSP streaming Project
* Author        : Seo, SeokWon 
* e-mail        : kueony@gmail.com
* Licence       : See the Copyright
*********************************************************************************/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <ctype.h>

#include "lib_system_network.h"

#define ETHERNET_NAME	"eth0"


int	GetMacAddr(char *a_pcMac)
{
    int				l_iSock;
    struct ifreq	stNet;
    unsigned char	*l_pcMac = NULL;
	int				i;

    if((l_iSock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        return -1;
    }

    memset(&stNet, 0, sizeof(stNet));

    sprintf(stNet.ifr_name, ETHERNET_NAME);

    if(ioctl(l_iSock, SIOCGIFHWADDR, &stNet) < 0)
    {
		close(l_iSock);
        return -1;
    }
	
	close(l_iSock);

    l_pcMac = (unsigned char *) stNet.ifr_hwaddr.sa_data;

    sprintf(a_pcMac, "%02x-%02x-%02x-%02x-%02x-%02x", l_pcMac[0] & 0xff, l_pcMac[1] & 0xff, l_pcMac[2] & 0xff, l_pcMac[3] & 0xff, l_pcMac[4] & 0xff, l_pcMac[5] & 0xff);

	for(i = 0 ; i < (int)strlen(a_pcMac) ; i++)
	{
		if(islower(a_pcMac[i]))
		{
			a_pcMac[i] = toupper(a_pcMac[i]);
		}
	}

	return 0;
}


int GetIpAddr(char *a_pcIp)
{
    int				l_iSock;
    struct ifreq	stNet;
    unsigned char	*l_pcIp = NULL;

    if((l_iSock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        return -1;
    }

    memset(&stNet, 0, sizeof(stNet));

    sprintf(stNet.ifr_name, ETHERNET_NAME);

    if(ioctl(l_iSock, SIOCGIFADDR, &stNet) < 0)
    {
		close(l_iSock);
        return -1;
    }

	close(l_iSock);

    l_pcIp = (unsigned char *) stNet.ifr_hwaddr.sa_data;

    sprintf(a_pcIp, "%d.%d.%d.%d", l_pcIp[2], l_pcIp[3], l_pcIp[4], l_pcIp[5]);

    return 0;
}

